package com.PomeloMusic.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import api.BroadcastCode;

import com.PomeloMusic.aidl.PlayerServiceHelper;
import com.PomeloMusic.aidl.PlayerUIHelper;
import com.PomeloMusic.controller.PlayerController;
import com.PomeloMusic.helper.PlayerState;
import com.PomeloMusic.helper.SongInfomation;
import com.PomeloMusic.helper.SystemState;
import com.PomeloMusic.service.PlayerService;
import com.PomeloMusic.util.PicturesEffectUtils;
import com.PomeloMusic.util.ProgressWheel;

//import com.PomeloMusic.view.LrcView;

public class PlayerActivity extends MMAbstractActivity implements
		OnTouchListener {

	/* UIs */
	private Animation operatingAnim;

	private static final int ANGLE_ROUND = 5000;
	private Button playButton;
	private Button nextButton;
	private Button channelButton;
	private Button loveButton;
	private ProgressWheel waitProgressBar;
	private ImageView songImageView;
	private TextView songTitle;
	private TextView songTime;
	private ProgressWheel musicProgressBar;
	private FrameLayout songImageLayout;
	// private LrcView lyricView;
	private Notification notification;
	private NotificationManager nm;
	private int maxTime = 1;
	/* BroadcastReceivers */
	private BroadcastReceiver lrcReceiver;

	/* Listeners */
	private View.OnClickListener startListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			PlayerController playerController = ((PlayerController) controller);
			playerController.onStart();
			// manager.e(PlayerActivity.this);
		}
	};

	private View.OnClickListener stopListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			PlayerController playerController = ((PlayerController) controller);
			playerController.onPause();
		}
	};

	private View.OnClickListener nextListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			songImageView.clearAnimation();
			songImageView.setImageResource(R.drawable.background_null);

			PlayerController playerController = ((PlayerController) controller);
			// gestureListener.refreshImage();
			playerController.onNext();
		}
	};

	private View.OnClickListener loveListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			loveButton.setEnabled(false);
			PlayerController playerController = ((PlayerController) controller);
			playerController.onLove();
		}
	};

	private View.OnClickListener unloveListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			loveButton.setEnabled(false);
			PlayerController playerController = ((PlayerController) controller);
			playerController.onUnLove();
		}
	};

	private View.OnClickListener channelListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			PlayerController playerController = ((PlayerController) controller);
			playerController.onchannel();
		}
	};

	/* ********* */
	private Handler stateHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Resources res = PlayerActivity.this.getResources();
			switch (msg.what) {
			case PlayerState.PLAY: {
				playButton.setBackgroundDrawable(res
						.getDrawable(R.drawable.selector_pausebtn_style));
				playButton.setOnClickListener(stopListener);
				rotateView();
				break;
			}
			case PlayerState.PAUSE: {
				playButton.setBackgroundDrawable(res
						.getDrawable(R.drawable.selector_playbutton_style));
				playButton.setOnClickListener(startListener);
				songImageView.clearAnimation();
				break;
			}
			case PlayerState.STOP: {
				playButton.setBackgroundDrawable(res
						.getDrawable(R.drawable.selector_playbutton_style));
				playButton.setOnClickListener(startListener);
				break;
			}
			case PlayerState.WAIT: {
				waitProgressBar.setVisibility(View.VISIBLE);
				playButton.setEnabled(false);
				nextButton.setEnabled(false);
				loveButton.setEnabled(false);

				// lyricView.clear(); // Clear lrc view
				break;
			}
			case PlayerState.READY: {
				waitProgressBar.setVisibility(View.GONE);
				playButton.setEnabled(true);
				nextButton.setEnabled(true);
				loveButton.setEnabled(true);
				break;
			}
			case PlayerState.PROGRESS_MAX: { // 上限
				int max = msg.arg1;
				maxTime = max;
				rotateView();
				// musicProgressBar.setBarLength(360 / max);// setMax(max)
				break;
			}
			case PlayerState.PROGRESS: { // 一级
				int position = msg.arg1;
				musicProgressBar.setProgress(position * 360 / maxTime);
				if (operatingAnim != null && songImageView != null
						&& !operatingAnim.hasStarted()) {
					rotateView();
				}

				// lyricView.updateIndex(position < 1 ? 100 : position * 1000);

				break;
			}
			case PlayerState.BUFFERING: { // 二级
				// int position = msg.arg1;
				// musicProgressBar.setProgress(position);
				break;
			}
			case PlayerState.LOVE: {
				loveButton.setBackgroundDrawable(res
						.getDrawable(R.drawable.selector_islovebtn_style));
				loveButton.setOnClickListener(unloveListener);
				break;
			}
			case PlayerState.UNLOVE: {
				loveButton.setBackgroundDrawable(res
						.getDrawable(R.drawable.selector_lovebtn_style));
				loveButton.setOnClickListener(loveListener);
				break;
			}
			case PlayerState.ENABLE_LOVE: {
				loveButton.setEnabled(true);
				break;
			}
			case PlayerState.DISABLE_LOVE: {
				loveButton.setEnabled(false);
				break;
			}
			}
			super.handleMessage(msg);
		}
	};

	private Handler systemHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case SystemState.NET_WORK_ERROR: {
				String message = (String) msg.obj;
				Toast.makeText(getApplicationContext(), "" + message,
						Toast.LENGTH_LONG).show();
				break;
			}
			}
			super.handleMessage(msg);
		}

	};

	private Handler songInfomationHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case SongInfomation.IMAGE: {

				songImageView.clearAnimation();
                
				Bitmap bitmap = (Bitmap) msg.obj;
				// Bitmap bitmap1  =BitmapFactory.decodeResource(getResources(), R.drawable.dc_bg);
				songImageView.setImageBitmap(PicturesEffectUtils.toRoundCorner(
						bitmap, ANGLE_ROUND));

				/**
				 * Start album image animation
				 */
				Animation animation = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.mp_songimage_animation);
				songImageView.startAnimation(animation);

				break;
			}
			case SongInfomation.TITLE: {
				String title = (String) msg.obj;

				/**
				 * Update the song information in activity
				 */
				songTitle.setText(title);

				notification.tickerText = "正在播放：" + title;
				RemoteViews rv = notification.contentView;
				rv.setTextViewText(R.id.notificationStatusText, "正在播放：" + title);
				nm.cancel(R.string.app_name);
				nm.notify(R.string.app_name, notification);
				break;
			}
			case SongInfomation.TIME: {
				String time = (String) msg.obj;
				songTime.setText("" + time);
				break;
			}
			}
			super.handleMessage(msg);
		}

	};

	public Handler getStateHandler() {
		return stateHandler;
	}

	public Handler getSystemHandler() {
		return systemHandler;
	}

	public Handler getSongInfomationHandler() {
		return songInfomationHandler;
	}

	/* Service Interface */
	private PlayerServiceHelper serviceHelper;

	/* Connection */
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			serviceHelper = PlayerServiceHelper.Stub.asInterface(service);
			try {
				serviceHelper.setPlayerUIHelper(new UIHelper());
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			PlayerController playerController = ((PlayerController) controller);
			playerController.setPlayHelper(serviceHelper);
		}

		public void onServiceDisconnected(ComponentName className) {
			Log.i("Tag", "disconnect service");
			serviceHelper = null;
		}
	};

	public PlayerServiceHelper getServiceHelper() {
		return serviceHelper;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setWindowProperties();
		setContentView(R.layout.player);
		setController(new PlayerController(this));
		setupViews();
		setupBroadcastReceiver();
	}

	private void setWindowProperties() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().setFlags(~WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		Intent serviceIntent = new Intent(getApplicationContext(),
				PlayerService.class);
		bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);

		IntentFilter intentFilter = new IntentFilter(BroadcastCode.DOWNLOAD_LRC);
		registerReceiver(lrcReceiver, intentFilter);
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		PlayerActivity.this.unbindService(mConnection);

		unregisterReceiver(lrcReceiver);
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menuAlert();
		return false;
	}

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// int itemId = item.getItemId();
	// switch (itemId) {
	// case R.id.exit:
	// HomeAllActivity.this.finish();
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }

	private void menuAlert() {

		final AlertDialog alertDialog = new AlertDialog.Builder(
				PlayerActivity.this).create();
		alertDialog.show();
		Window window = alertDialog.getWindow();
		// 设置dialog位置
		WindowManager.LayoutParams layoutParams = alertDialog.getWindow()
				.getAttributes();
		layoutParams.gravity = (Gravity.CENTER);
		alertDialog.getWindow().setAttributes(layoutParams);

		window.setContentView(R.layout.dialog_menu);
		window.setBackgroundDrawableResource(android.R.color.transparent);
		Button hate = (Button) window.findViewById(R.id.btn_dialog_left_top);
		Button setting = (Button) window
				.findViewById(R.id.btn_dialog_right_top);
		Button about = (Button) window
				.findViewById(R.id.btn_dialog_left_bottom);
		Button exit = (Button) window
				.findViewById(R.id.btn_dialog_right_bottom);

		hate.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				PlayerController playerController = ((PlayerController) controller);
				playerController.neverPlay();
				alertDialog.cancel();
			}
		});

		setting.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.cancel();
			}
		});

		about.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PlayerActivity.this,
						AboutActivity.class);
				startActivity(intent);
				alertDialog.cancel();
			}
		});

		exit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.cancel();
				exitAlert();
			}
		});

	}

	private void exitAlert() {

		final AlertDialog alertDialog = new AlertDialog.Builder(
				PlayerActivity.this).create();
		alertDialog.show();
		Window window = alertDialog.getWindow();
		// 设置dialog位置
		WindowManager.LayoutParams layoutParams = alertDialog.getWindow()
				.getAttributes();
		layoutParams.gravity = (Gravity.CENTER);
		alertDialog.getWindow().setAttributes(layoutParams);

		window.setContentView(R.layout.dialog_exit);
		window.setBackgroundDrawableResource(android.R.color.transparent);
		Button sure = (Button) window.findViewById(R.id.btn_dialog_left);
		Button cancel = (Button) window.findViewById(R.id.btn_dialog_right);

		sure.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				PlayerActivity.this.finish();
				((PlayerController) controller).close();
				Intent intent = new Intent(getApplicationContext(),
						PlayerService.class);
				nm.cancelAll();
				getApplicationContext().stopService(intent);
			}
		});

		cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.cancel();
			}
		});
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent home = new Intent(Intent.ACTION_MAIN);
		home.addCategory(Intent.CATEGORY_HOME);
		startActivity(home);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == Activity.RESULT_OK) {
			Bundle bundle = data.getBundleExtra("bundle");
			PlayerController playerController = ((PlayerController) controller);
			playerController.setUserData(bundle);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/***
	 * IPC interface for activity
	 * 
	 * @author Coffee
	 */
	class UIHelper extends PlayerUIHelper.Stub {
		@Override
		public void showWaitBar(boolean show) throws RemoteException {
			// TODO Auto-generated method stub
			Message msg = stateHandler.obtainMessage();
			msg.what = show ? PlayerState.WAIT : PlayerState.READY;
			stateHandler.sendMessage(msg);
		}

		@Override
		public void setMusicProgressBarMax(int max) throws RemoteException {
			// TODO Auto-generated method stub
			Message msg = stateHandler.obtainMessage();
			msg.what = PlayerState.PROGRESS_MAX;
			msg.arg1 = max;
			stateHandler.sendMessage(msg);
		}

		@Override
		public void updateMusicProgress(int position) throws RemoteException {
			// TODO Auto-generated method stub
			Message msg = stateHandler.obtainMessage();
			msg.what = PlayerState.PROGRESS;
			msg.arg1 = position;
			stateHandler.sendMessage(msg);
		}

		@Override
		public void updateBufferingProgress(int position)
				throws RemoteException {
			// TODO Auto-generated method stub
			Message msg = stateHandler.obtainMessage();
			msg.what = PlayerState.BUFFERING;
			msg.arg1 = position;
			stateHandler.sendMessage(msg);
		}

		@Override
		public void updateLrctime(long time) throws RemoteException {
			// TODO Auto-generated method stub
			Message msg = stateHandler.obtainMessage();
			msg.what = PlayerState.LRC_UPDATE;
			msg.obj = time;
			stateHandler.sendMessage(msg);
		}

	}

	@Override
	protected void setupViews() {
		// TODO Auto-generated method stub
		this.playButton = (Button) findViewById(R.id.playbtn);
		this.nextButton = (Button) findViewById(R.id.nextbtn);
		this.channelButton = (Button) findViewById(R.id.channelBtn);
		this.waitProgressBar = (ProgressWheel) findViewById(R.id.waitProgressBar);
		this.songImageView = (ImageView) findViewById(R.id.songimageview);
		this.songTime = (TextView) findViewById(R.id.songtime);
		this.songTitle = (TextView) findViewById(R.id.songtitle);
		this.musicProgressBar = (ProgressWheel) findViewById(R.id.musicProgressbar);
		this.loveButton = (Button) findViewById(R.id.lovebtn);
		this.songImageLayout = (FrameLayout) findViewById(R.id.songimageframe);
		// this.lyricView = (LrcView) findViewById(R.id.lrctextview);
		this.waitProgressBar.spin();
		this.nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		this.notification = new Notification(R.drawable.ic_launcher, "正在播放：",
				System.currentTimeMillis());

		this.songImageLayout.setOnTouchListener(this);
		this.songImageLayout.setLongClickable(true);
		operatingAnim = AnimationUtils.loadAnimation(this,
				R.anim.rotate_animation);
		// gestureListener = new PlayerAlbumGestureListener(songImageView,
		// lyricView, this);
		// mGestureDetector = new GestureDetector(gestureListener);

		/***
		 * 初始化通知栏
		 */
		notification.flags = Notification.FLAG_ONGOING_EVENT;
		Intent i = new Intent(getApplicationContext(), PlayerActivity.class);
		i.setAction(Intent.ACTION_MAIN);
		i.addCategory(Intent.CATEGORY_LAUNCHER);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent contentIntent = PendingIntent.getActivity(
				getApplication(), R.string.app_name, i,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.contentView = new RemoteViews(
				PlayerActivity.this.getPackageName(), R.layout.notification);
		notification.contentIntent = contentIntent;
		super.setupViews();
	}

	protected void setupBroadcastReceiver() {
		lrcReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				// LyricModel lyric = (LyricModel) intent
				// .getSerializableExtra("lyric");
				// lyricView.setLyric(lyric);

			}

		};
	}

	@Override
	protected void setListeners() {
		// TODO Auto-generated method stub
		this.playButton.setOnClickListener(startListener);
		this.nextButton.setOnClickListener(nextListener);
		this.loveButton.setOnClickListener(loveListener);
		this.channelButton.setOnClickListener(channelListener);
	}

	/***
	 * 识别滑动手势
	 */
	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		// TODO Auto-generated method stub
		// return mGestureDetector.onTouchEvent(event);
		return false;
	};

	private void rotateView() {

		LinearInterpolator lin = new LinearInterpolator();
		operatingAnim.setInterpolator(lin);

		if (operatingAnim != null) {
			songImageView.startAnimation(operatingAnim);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);

		if (operatingAnim != null && songImageView != null
				&& operatingAnim.hasStarted()) {
			songImageView.clearAnimation();
			songImageView.startAnimation(operatingAnim);
		}
	}

}
